package com.sgcc.fj.pmos.common.widget;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;

import com.sgcc.pmos.nativesdk.R;


public class TitleBar extends LinearLayout implements View.OnClickListener {

    private Activity mActivity;
    public ImageView ivBack;
    public TextView tvTitle;
    private ViewGroup layoutRight;
    private ImageView ivRight, ivRight1;
    public TextView tvRight;
    private boolean isVisible = true;
    private String title_text;
    private String title_right;
    private int text_right_color;
    private View line;
    private RelativeLayout titlebar_layout;
    /**
     * 自己取处理返回结果的回调对象
     */
    private OnClickListener backOnClickListener;
    private OnClickListener rightOnClickListener;
    private OnClickListener right1OnClickListener;

    public TitleBar(Context context) {
        super(context);
        init(context, null);
    }

    public TitleBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public TitleBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray a =
                    getContext().obtainStyledAttributes(attrs, R.styleable.Headerbar,
                            -1, 0);
            isVisible = a.getBoolean(R.styleable.Headerbar_isVisibleLine, isVisible);
            title_text = a.getString(R.styleable.Headerbar_text);
            title_right = a.getString(R.styleable.Headerbar_text_right);
            text_right_color = a.getColor(R.styleable.Headerbar_text_right_color, getResources().getColor(R.color._333333));
        }
        if (context == null) {
            return;
        } else if (context instanceof Activity) {
            this.mActivity = (Activity) context;
        } else if (context instanceof ContextWrapper) {
            if (context instanceof Activity) {
                this.mActivity = (Activity) context;
            }
            context = ((ContextWrapper) context).getBaseContext();
        }
        inflate(context, R.layout.widget_title_bar, this);

        titlebar_layout = findViewById(R.id.titlebar_layout);
        ivBack = findViewById(R.id.ivBack);
        ivBack.setOnClickListener(this);
        tvTitle = findViewById(R.id.tvTitle);
        layoutRight = findViewById(R.id.layoutRight);
        ivRight = findViewById(R.id.ivRight);
        tvRight = findViewById(R.id.tvRight);
        ivRight1 = findViewById(R.id.ivRight1);
        line = findViewById(R.id.line);
        if (!TextUtils.isEmpty(title_text)) {
            tvTitle.setText(title_text);
        }
        if (isVisible) {
            line.setVisibility(VISIBLE);
        } else {
            line.setVisibility(GONE);
        }
        if (!TextUtils.isEmpty(title_right)) {
            layoutRight.setVisibility(VISIBLE);
            tvRight.setVisibility(VISIBLE);
            tvRight.setText(title_right);
            if (text_right_color != 0) {
                tvRight.setTextColor(text_right_color);
            }
        } else {
            tvRight.setVisibility(GONE);
            layoutRight.setVisibility(GONE);
        }

    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.ivBack) {
            if (null != backOnClickListener) {
                backOnClickListener.onClick(v);
                return;
            }
            if (mActivity != null) {
                mActivity.finish();
            }
        } else if (v.getId() == R.id.ivRight) {
            if (null != rightOnClickListener) {
                rightOnClickListener.onClick(v);
                return;
            }
        } else if (v.getId() == R.id.ivRight1) {
            if (null != right1OnClickListener) {
                right1OnClickListener.onClick(v);
                return;
            }
        }
    }

    public void setBackground_color(int background_color) {
        try {
            if (background_color == R.color.white) {
                titlebar_layout.setBackgroundResource(R.color.white);
                ivBack.setImageResource(R.mipmap.icon_back);
                tvTitle.setTextColor(getResources().getColor(R.color._333333));
                tvRight.setTextColor(getResources().getColor(R.color._333333));
            } else {
                titlebar_layout.setBackgroundResource(background_color);
                ivBack.setImageResource(R.mipmap.icon_back_w);
                tvTitle.setTextColor(getResources().getColor(R.color.white));
                tvRight.setTextColor(getResources().getColor(R.color.white));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setTitle(String title) {
        tvTitle.setText(title);
        tvTitle.getPaint().setFakeBoldText(true);
    }

    public void setTitle(@StringRes int title) {
        tvTitle.setText(title);
        tvTitle.getPaint().setFakeBoldText(true);
    }

    public void setTitleBold() {
        tvTitle.getPaint().setFakeBoldText(true);
    }

    public void setRightText(@StringRes int text, OnClickListener listener) {
        tvRight.setText(text);
        tvRight.setVisibility(VISIBLE);
        layoutRight.setOnClickListener(listener);
        layoutRight.setVisibility(VISIBLE);
    }

    public void setRightOnclickListener(OnClickListener listener) {
        this.rightOnClickListener = listener;
    }

    public void setRight1OnclickListener(OnClickListener listener) {
        this.right1OnClickListener = listener;
    }

    public void setRightText(String text, OnClickListener listener) {
        tvRight.setText(text);
        tvRight.setVisibility(VISIBLE);
        tvRight.setOnClickListener(listener);
        layoutRight.setVisibility(VISIBLE);
    }

    public void setRightIcon(@DrawableRes int icon, OnClickListener listener) {
        ivRight.setImageResource(icon);
        ivRight.setVisibility(VISIBLE);
        ivRight.setOnClickListener(listener);
        layoutRight.setVisibility(VISIBLE);
    }

    public void setRight1Icon(@DrawableRes int icon, OnClickListener listener) {
        ivRight1.setImageResource(icon);
        ivRight1.setVisibility(VISIBLE);
        ivRight1.setOnClickListener(listener);
        layoutRight.setVisibility(VISIBLE);
    }

    public void setRightIconAndText(@DrawableRes int icon, String text, OnClickListener listener) {
        ivRight.setImageResource(icon);
        ivRight.setVisibility(VISIBLE);
        tvRight.setText(text);
        tvRight.setVisibility(VISIBLE);
        layoutRight.setOnClickListener(listener);
        layoutRight.setVisibility(VISIBLE);
    }

    /**
     * 设置自己的 返回监听
     *
     * @param backOnClickListener 监听对象
     */
    public void setBackOnClickListener(OnClickListener backOnClickListener) {
        this.backOnClickListener = backOnClickListener;
    }

    public void onBackClick(OnClickListener listener) {
        ivBack.setOnClickListener(listener);
    }

}
